package com.suarte.webapp.action;

import com.suarte.core.Currency;
import java.io.Serializable;
import org.appfuse.service.GenericManager;

/**
 * @date   Dec 21, 2010
 * @author Ggutierrez
 */
public class CurrencyForm extends BasePage implements Serializable {
    private GenericManager<Currency, Long> currencyManager;
    private Currency currency = new Currency();
    private Long id;

    public void setCurrencyManager(GenericManager<Currency, Long> currencyManager) {
        this.currencyManager = currencyManager;
    }

    public Currency getCurrency() {
        return currency;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        currencyManager.remove(currency.getId());
        addMessage("currency.deleted");

        return "list";
    }

    public String edit() {
        if (id != null) {
            currency = currencyManager.get(id);
        } else {
            currency = new Currency();
        }

        return "edit";
    }

    public String add() {
        currency = new Currency();

        return "add";
    }

    public String save() {
        boolean isNew = (currency.getId() == null);
        currencyManager.save(currency);

        String key = (isNew) ? "currency.added" : "currency.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
}
